<?php

namespace app\api\controller;

use think\Controller;

class Login extends Controller
{
    /**
     * 注册
     */
    public function register($header, $data, $user)
    {
        $check=check_array($data,array('mobile','verifi','password'));
        if($check['code']!=1){
            return $check;
        }
        $mobile = $data['mobile'];
        //用户手机号
        $check = check_format($mobile, config('regular_str.mobile'));
        if (!$check) {
            return array('code' => 2, 'msg' => '手机号格式出错', 'data' => array());
        }
        $verifi = $data['verifi'];
        $check = check_format($verifi, config('regular_str.verifi'));
        if (!$check) {
            return array('code' => 2, 'msg' => '验证码格式出错', 'data' => array());
        }
        $password = $data['password'];
        $check = check_format($password, config('regular_str.password'));
        if (!$check) {
            return array('code' => 2, 'msg' => '登录密码格式出错', 'data' => array());
        }
        $t_verifi=db('sms')
            ->where(array('mobile'=>$mobile,'u_type'=>1,'verifi'=>$verifi,'u_status'=>1,'end_time'=>array('gt',time())))
            ->find();
        if(empty($t_verifi)){
            return array('code' => 2, 'msg' => '验证码错误或已过期', 'data' => array());
        }
        $user=db('user')
            ->where(array('u_mobile'=>$mobile))
            ->find();
        if(!empty($user)){
            return array('code' => 2, 'msg' => '该用户已注册', 'data' => array());
        }
        $data1=array('u_mobile'=>$mobile,'u_pwd'=>md5(md5($password)),'u_register_time'=>time(),'u_register_type'=>$header['phone_model'],'u_last_login_ip'=>$_SERVER['REMOTE_ADDR'],'u_status'=>1);
        $token=md5(time().rand(100000,999999));
        $data1['u_token']=$token;
        $data1['u_over_time']=time()+7*24*3600;
        $res=db('user')->insertGetId($data1);
        if($res>0){
            db('sms')
                ->where(array('mobile'=>$mobile,'u_type'=>1,'verifi'=>$verifi,'u_status'=>1))
                ->update(array('u_status'=>2,'use_time'=>time()));
            $coupon=db('coupon')
                ->where(array('c_status'=>1,'c_type'=>1,'c_end_time'=>array('gt',time()+24*3600),'c_start_time'=>array('lt',time()),'c_number'=>array('gt',0)))
                ->order("c_id desc")
                ->find();
            //注册赠送优惠券
            if(!empty($coupon)){
                $data2=array('u_id'=>$res,'c_id'=>$coupon['c_id'],'uc_discount'=>$coupon['c_discount'],'uc_min_buy'=>$coupon['c_min_buy'],'uc_create_time'=>time(),'uc_start_time'=>$coupon['c_start_time'],'uc_end_time'=>$coupon['c_end_time'],'uc_type'=>1,'uc_status'=>1);
                $data3=array();
                for($i=1;$i<=$coupon['c_number'];$i++){
                    $data3[]=$data2;
                }
                db('user_coupon')
                    ->insertAll($data3);
                //添加用户优惠券
                $data4=array('ucl_create_time'=>time(),'u_id'=>$res,'c_id'=>$coupon['c_id'],'c_num'=>$coupon['c_number'],'c_type'=>1);
                db('user_coupon_log')
                    ->insertGetId($data4);
                //增加优惠卷记录
                db('coupon')
                    ->where(array('c_id'=>$coupon['c_id'],'c_status'=>1))
                    ->setInc('u_grant_number',$coupon['c_number']);
                //增加优惠券兑换量
            }
            return array('code' => 1, 'msg' => '注册成功', 'data' => array('token'=>$token,'uid'=>$res));
        }else{
            return array('code' => 2, 'msg' => '注册失败，请重试', 'data' => array());
        }
    }
    /**
     * 密码登录
     */
    public function login_pwd($header, $data, $user)
    {
        $check=check_array($data,array('mobile','password'));
        if($check['code']!=1){
            return $check;
        }
        $mobile = $data['mobile'];
        //用户手机号
        $check = check_format($mobile, config('regular_str.mobile'));
        if (!$check) {
            return array('code' => 2, 'msg' => '手机号格式出错', 'data' => array());
        }
        $password = $data['password'];
        $check = check_format($password, config('regular_str.password'));
        if (!$check) {
            return array('code' => 2, 'msg' => '登录密码格式出错', 'data' => array());
        }
        $user=db('user')
            ->where(array('u_mobile'=>$mobile))
            ->find();
        if(empty($user)){
            return array('code' => 2, 'msg' => '该手机号还没有注册', 'data' => array());
        }
        if($user['u_pwd']!=md5(md5($password))){
            return array('code' => 2, 'msg' => '密码错误请重试', 'data' => array());
        }
        if($user['u_status']!=1){
            return array('code' => 2, 'msg' => '账户已被冻结', 'data' => array());
        }
        $token=md5(time().rand(100000,999999).'_'.$user['u_id']);
        $data1['u_token']=$token;
        $data1['u_over_time']=time()+7*24*3600;
        $res=db('user')->where(array('u_mobile'=>$mobile))->update($data1);
        if($res!==false){
            return array('code' => 1, 'msg' => '登录成功', 'data' => array('token'=>$token,'uid'=>$user['u_id']));
        }else{
            return array('code' => 2, 'msg' => '登录失败，请重试', 'data' => array());
        }
    }
    /**
     * 短信登录
     */
    public function login_verifi($header, $data, $user)
    {
        $check=check_array($data,array('mobile','verifi'));
        if($check['code']!=1){
            return $check;
        }
        $mobile = $data['mobile'];
        //用户手机号
        $check = check_format($mobile, config('regular_str.mobile'));
        if (!$check) {
            return array('code' => 2, 'msg' => '手机号格式出错', 'data' => array());
        }
        $verifi = $data['verifi'];
        $check = check_format($verifi, config('regular_str.verifi'));
        if (!$check) {
            return array('code' => 2, 'msg' => '验证码格式出错', 'data' => array());
        }
        $t_verifi=db('sms')
            ->where(array('mobile'=>$mobile,'u_type'=>3,'verifi'=>$verifi,'u_status'=>1,'end_time'=>array('gt',time())))
            ->find();
        if(empty($t_verifi)){
            return array('code' => 2, 'msg' => '验证码错误或已过期', 'data' => array());
        }
        $user=db('user')
            ->where(array('u_mobile'=>$mobile))
            ->find();
        if(empty($user)){
            return array('code' => 2, 'msg' => '该手机号还没有注册', 'data' => array());
        }
        if($user['u_status']!=1){
            return array('code' => 2, 'msg' => '账户已被冻结', 'data' => array());
        }
        $token=md5(time().rand(100000,999999).'_'.$user['u_id']);
        $data1['u_token']=$token;
        $data1['u_over_time']=time()+7*24*3600;
        $res=db('user')->where(array('u_mobile'=>$mobile))->update($data1);
        if($res!==false){
            return array('code' => 1, 'msg' => '登录成功', 'data' => array('token'=>$token,'uid'=>$user['u_id']));
        }else{
            return array('code' => 2, 'msg' => '登录失败，请重试', 'data' => array());
        }
    }
    /**
     * 修改密码
     */
    public function repassword($header, $data, $user)
    {
        $check=check_array($data,array('mobile','verifi','password'));
        if($check['code']!=1){
            return $check;
        }
        $mobile = $data['mobile'];
        //用户手机号
        $check = check_format($mobile, config('regular_str.mobile'));
        if (!$check) {
            return array('code' => 2, 'msg' => '手机号格式出错', 'data' => array());
        }
        $verifi = $data['verifi'];
        $check = check_format($verifi, config('regular_str.verifi'));
        if (!$check) {
            return array('code' => 2, 'msg' => '验证码格式出错', 'data' => array());
        }
        $password = $data['password'];
        $check = check_format($password, config('regular_str.password'));
        if (!$check) {
            return array('code' => 2, 'msg' => '登录密码格式出错', 'data' => array());
        }
        $t_verifi=db('sms')
            ->where(array('mobile'=>$mobile,'u_type'=>2,'verifi'=>$verifi,'u_status'=>1,'end_time'=>array('gt',time())))
            ->find();
        if(empty($t_verifi)){
            return array('code' => 2, 'msg' => '验证码错误或已过期', 'data' => array());
        }
        $user=db('user')
            ->where(array('u_mobile'=>$mobile))
            ->find();
        if(empty($user)){
            return array('code' => 2, 'msg' => '该手机号还没有注册', 'data' => array());
        }
        $res=db('user')->where(array('u_mobile'=>$mobile))->update(array('u_pwd'=>md5(md5($password))));
        if($res!==false){
            db('sms')
                ->where(array('mobile'=>$mobile,'u_type'=>2,'verifi'=>$verifi,'u_status'=>1))
                ->update(array('u_status'=>2,'use_time'=>time()));
            return array('code' => 1, 'msg' => '密码修改成功', 'data' => array());
        }else{
            return array('code' => 2, 'msg' => '密码修改', 'data' => array());
        }
    }
    /**
     * 微信登录
     */
    function login_wx($header, $data, $user){
        $check=check_array($data,array('openid'));
        if($check['code']!=1){
            return $check;
        }
        $openid=$data['openid'];
        $user=db('user')->where(array('open_id'=>$openid))->find();
        if(!empty($user)){
            $token=md5(time().rand(100000,999999).'_'.$user['u_id']);
            $data1['u_token']=$token;
            $data1['u_over_time']=time()+7*24*3600;
            $res=db('user')->where(array('u_id'=>$user['u_id']))->update($data1);
            if($res!==false){
                return array('code' => 1, 'msg' => '登录成功', 'data' => array('token'=>$token,'uid'=>$user['u_id'],'openid'=>''));
            }else{
                return array('code' => 2, 'msg' => '登录失败，请重试', 'data' => array());
            }
        }else{
            return array('code' => 1, 'msg' => '登录成功', 'data' => array('token'=>'','uid'=>'','openid'=>$openid));
        }
    }
    /**
     * 微信认证
     */
    function register_wx($header, $data, $user){
        $check=check_array($data,array('mobile','verifi','openid'));
        if($check['code']!=1){
            return $check;
        }
        $openid=$data['openid'];
        $mobile = $data['mobile'];
        //用户手机号
        $check = check_format($mobile, config('regular_str.mobile'));
        if (!$check) {
            return array('code' => 2, 'msg' => '手机号格式出错', 'data' => array());
        }
        $verifi = $data['verifi'];
        $check = check_format($verifi, config('regular_str.verifi'));
        if (!$check) {
            return array('code' => 2, 'msg' => '验证码格式出错', 'data' => array());
        }
        $t_verifi=db('sms')
            ->where(array('mobile'=>$mobile,'u_type'=>4,'verifi'=>$verifi,'u_status'=>1,'end_time'=>array('gt',time())))
            ->find();
        if(empty($t_verifi)){
            return array('code' => 2, 'msg' => '验证码错误或已过期', 'data' => array());
        }
        $user=db('user')
            ->where(array('u_mobile'=>$mobile))
            ->find();
        if(!empty($user)){
            //已有用户
            $token=md5(time().rand(100000,999999).'_'.$user['u_id']);
            $data1['u_token']=$token;
            $data1['u_over_time']=time()+7*24*3600;
            $data1['open_id']=$openid;
            $res=db('user')->where(array('u_mobile'=>$mobile))->update($data1);
            db('sms')
                ->where(array('mobile'=>$mobile,'u_type'=>4,'verifi'=>$verifi,'u_status'=>1))
                ->update(array('u_status'=>2,'use_time'=>time()));
            return array('code' => 1, 'msg' => '认证成功', 'data' => array('token'=>$token,'uid'=>$user['u_id']));
        }
        $data1=array('u_mobile'=>$mobile,'u_pwd'=>md5(md5(rand(0,9999999999))),'u_register_time'=>time(),'u_register_type'=>$header['phone_model'],'u_last_login_ip'=>$_SERVER['REMOTE_ADDR'],'u_status'=>1,'open_id'=>$openid);
        if(isset($data['head_img'])){
            $data1['u_head_img']=$data['head_img'];
        }
        $token=md5(time().rand(100000,999999));
        $data1['u_token']=$token;
        $data1['u_over_time']=time()+7*24*3600;
        $res=db('user')->insertGetId($data1);
        if($res>0){
            db('sms')
                ->where(array('mobile'=>$mobile,'u_type'=>4,'verifi'=>$verifi,'u_status'=>1))
                ->update(array('u_status'=>2,'use_time'=>time()));
            $coupon=db('coupon')
                ->where(array('c_status'=>1,'c_type'=>1,'c_end_time'=>array('gt',time()+24*3600),'c_start_time'=>array('lt',time()),'c_number'=>array('gt',0)))
                ->order("c_id desc")
                ->find();
            //注册赠送优惠券
            if(!empty($coupon)){
                $data2=array('u_id'=>$res,'c_id'=>$coupon['c_id'],'uc_discount'=>$coupon['c_discount'],'uc_min_buy'=>$coupon['c_min_buy'],'uc_create_time'=>time(),'uc_start_time'=>$coupon['c_start_time'],'uc_end_time'=>$coupon['c_end_time'],'uc_type'=>1,'uc_status'=>1);
                $data3=array();
                for($i=1;$i<=$coupon['c_number'];$i++){
                    $data3[]=$data2;
                }
                db('user_coupon')
                    ->insertAll($data3);
                //添加用户优惠券
                $data4=array('ucl_create_time'=>time(),'u_id'=>$res,'c_id'=>$coupon['c_id'],'c_num'=>$coupon['c_number'],'c_type'=>1);
                db('user_coupon_log')
                    ->insertGetId($data4);
                //增加优惠卷记录
                db('coupon')
                    ->where(array('c_id'=>$coupon['c_id'],'c_status'=>1))
                    ->setInc('u_grant_number',$coupon['c_number']);
                //增加优惠券兑换量
            }
            return array('code' => 1, 'msg' => '认证成功', 'data' => array('token'=>$token,'uid'=>$res));
        }else{
            return array('code' => 2, 'msg' => '认证成功，请重试', 'data' => array());
        }
    }
}